<?php
class Custom_Controller_Action_Admin_Controller extends Zend_Controller_Action {

	public $infoUser;
	public $languages;
	public $defaultLanguage;
	public $view;
	protected $acl;
	public $_moduleName;

	public function init() {
		$this->view->assign ( 'createable', '' );
		$this->view->assign ( 'copyable', '' );
		$this->view->assign ( 'deleteable', '' );
		$this->view->assign ( 'updateable', '' );
		$this->view->assign ( 'hastrash', '' );
		$this->view->assign ( 'moduleName', $this->_request->getModuleName() );
		$this->view->assign ( 'controllerName', $this->_request->getControllerName() );
		$this->view->menubox = '';
	}
	public function noaccessAction() {

	}

	public function preDispatch() {
		$this->_moduleName = $this->_request->getModuleName();
		$ns = new Zend_Session_Namespace('ThamzaSession');
		$thamzaAuth = new Zend_Session_Namespace('Thamza_Auth');
		if (! isset ($ns->loginer) || !isset($thamzaAuth->storage)) {
			if ($this->_request->getActionName () != 'login') {
				$this->_redirect ( '/'.$this->_moduleName.'/staff/login' );
			}
		} else {
			$this->infoUser = $ns->loginer;
			$language = new Cpanel_Model_DbTable_Language ( );

			$this->languages = $language->getLanguages($ns->loginer->staff_id, $this);

			foreach($this->languages as $l) {
				if($l['default'] == 1) {
					$this->defaultLanguage = $l['lang_id'];
				}
			}

			$this->view->languages = $this->languages;
			$this->view->loginname  = $ns->loginer->first_name;
			
			$thamzaAuth = new Zend_Session_Namespace('Thamza_Auth');
			foreach($thamzaAuth->storage->mygroups['group_permission'] as $per) {
				if(!empty($per['module_name']) && empty($per['controller_name']) && empty($per['action_name']) && $per['permission'] == 'deny'  && $per['group_id'] == $this->infoUser->groupid) {
					$this->_helper->flashMessenger->addMessage('You are can not access the '.$this->_request->getModuleName().' module.');
					$errorSession = new Zend_Session_Namespace('ErrorSession');
					$errorSession->message = 'You are can not access the '.$this->_request->getModuleName().' module.';
					$this->_redirect ( '/'.$this->_moduleName.'/error/noaccess' );
				}
				if($per['module_name'] == $this->_request->getModuleName() && $per['controller_name'] == $this->_request->getControllerName() && empty($per['action_name']) && $per['permission'] == 'deny'  && $per['group_id'] == $this->infoUser->groupid) {
					$this->_helper->flashMessenger->addMessage('You are can not access the '.$this->_request->getModuleName().' module.');
					$errorSession = new Zend_Session_Namespace('ErrorSession');
					$errorSession->message = 'You are can not access the '.$this->_request->getControllerName().' controller.';
					$this->_redirect ( '/'.$this->_moduleName.'/error/noaccess' );
				}
				if($per['module_name'] == $this->_request->getModuleName() && $per['controller_name'] == $this->_request->getControllerName() && $per['action_name'] == $this->_request->getActionName() && $per['permission'] == 'deny'  && $per['group_id'] == $this->infoUser->groupid) {
					$this->_helper->flashMessenger->addMessage('You are can not access the '.$this->_request->getModuleName().' module.');
					$errorSession = new Zend_Session_Namespace('ErrorSession');
					$errorSession->message = 'You are can not access the '.$this->_request->getActionName().' action.';
					$this->_redirect ( '/'.$this->_moduleName.'/error/noaccess' );
				}
			}
//			echo '<pre>';
//			print_r($thamzaAuth->storage->mygroups);
//			print_r($this->infoUser);
		}
		//		else {
		//
		//			$this->infoUser = $auth->getIdentity();
		//			if(!isset($this->infoUser->staff_id)) {
		//				$auth->clearIdentity ();
		//				$this->_redirect ( '/cpanel/staff/login' );
		//				exit;
		//			}
			
		//			$lgObj = Cpanel_Model_DbTable_Language::instance();
		//			$this->view->lg4pcs = $lgObj->getList();// Get all language to pro check which one can speak
		//
		//			$this->view->loginname  = $this->infoUser->first_name;
		//			$this->view->uname  = $this->infoUser->username;
		//			$this->view->issupper  = $this->infoUser->is_supper;
		//			$this->view->group  = $this->infoUser->groupid;
		//			if($this->infoUser->is_admin == 0) $this->view->disabled  = 'disabled="disabled"';
		//			else $this->view->disabled  = '';
		//			$this->view->isadmin = $this->infoUser->is_admin;
		//			if($this->infoUser->groupid == 1) {
		//				$this->view->classActDel  = 'actdelete';
		//				$this->view->classActDelLabel  = 'Delete';
		//				$this->view->classActRevert  = 'actrevert';
		//				$this->view->classActRevertLabel  = 'Revert';
		//			} else if($this->infoUser->groupid == 2) {
		//				$this->view->classActDel  = 'movetrash';
		//				$this->view->classActDelLabel  = 'Move to trash';
		//				$this->view->classActRevert  = 'hasnot';
		//				$this->view->classActRevertLabel  = 'Denied';
		//			} else {
		//				$this->view->classActDel  = 'hasnot';
		//				$this->view->classActDelLabel  = 'Denied';
		//				$this->view->classActRevert  = 'hasnot';
		//				$this->view->classActRevertLabel  = 'Denied';
		//			}
		//		}
	}
	public function ckfinderAction() {
		$this->_helper->layout->disableLayout ();
	}
	public function convert2ArrayObject($data, $fields) {
		$arrObject = array();
		foreach($data as $item) {
			foreach($fields as $field) {
				if(!empty($field)) {
					$arrObject[][$field] = $item;
				}
			}
		}
		return $arrObject;
	}

	public function sendEmail($email, $subject, $body) {
		$mail = new Zend_Mail();
		$mail -> addTo($email);
		$mail -> setSubject($subject);
		$mail -> setBodyHtml($body, 'UTF-8');

		//Send it!
		$sent = true;
		try {
			$mail -> send();
		} catch (Exception $e) {
			$sent = false;
		}

		//Do stuff (display error message, log it, redirect user, etc)
		if ($sent) {
			//TODO
		} else {
			//TODO
		}
	}

	public function genRandomString($length = 15) {
		$characters = '0123456789abcdefghijklmnopqrstuvwxyz!@#$%^&*ABCDEFGHIJKMLNOPQSRTUVXYZ';
		$string = '';
		for ($p = 0; $p < $length; $p++) {
			$string .= $characters[mt_rand(0, strlen($characters)-1)];
		}
		return $string;
	}
}
?>